# -*- encoding: utf-8 -*-
"""
File Name: router_connection
Description:
Author : 'lirui'
date: 2024/3/5
Contact: marjey_lee@163.com
Change Activity:
2024/3/5:
"""
from netmiko import ConnectHandler

import ll
from ll.utils.en_de import de_s
from projects.monitoring.dedicated_line_monitor.config.config import ROUTER_LOGIN_INFO

_KEY = 'networknetworknetwork'


def build_router_connection(router_name):
    """
    build router connection
    Args:
        router_name:

    Returns:

    """
    try:
        ll.get_logger().info(f'Try build router connection {router_name}')
        username = ROUTER_LOGIN_INFO[router_name]['username']
        password = ROUTER_LOGIN_INFO[router_name]['password']
        username = de_s(username, _KEY)
        password = de_s(password, _KEY)
        device = {
            'device_type': 'huawei',
            'ip': ROUTER_LOGIN_INFO[router_name]['ip'],
            'username': username,
            'password': password,
        }
        if 'ExtLine' in router_name:
            device['device_type'] = 'hp_comware'
        connection = ConnectHandler(**device)
        return connection
    except Exception as e:
        ll.get_logger().info(f'Failed to  build router connection {router_name} {e}')
        return None


def build_router_connection_mapping():
    """
    Build router connection
    Returns:

    """
    mapping = {}
    for router_name, _ in ROUTER_LOGIN_INFO.items():
        connection = build_router_connection(router_name)
        if connection is not None:
            mapping[router_name] = connection
        else:
            ll.get_logger().warning(f'Create connection failed: {router_name}')
    return mapping
